package j.util.jdbc.simple.metadata;

import j.util.New;
import j.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 数据表的元数据信息，这些元数据信息从实体类的注解中获取
 * 表中所指的列均为数据库中的字段名（均为大写字母），而不是实体中的属性名
 * @author 刘军海
 * @version Sep 12, 2011 8:26:27 PM
 *
 */
public class TableMetaData {

	private String tableName = "";//表名称
	//字段的值为自动生成，如sql中的自增长、oracle中的序列
	private Set<String> generatedKeyColumns;	
	//该表中的所有主键字段列表，若为复合主键，将用逗号隔开
	private String pkColumns;
	//该表中的所有唯一约束键，复合键用逗号隔开
	private List<String> ukColumns;
	//该表中所有的可用字段，key为数据库中的字段名，value为字段对象表示
	private Map<String,Column> columns;
	public TableMetaData(String schemaName, String tblName){
		if(StringUtils.isNotEmpty(schemaName)){
			this.tableName = schemaName.toUpperCase()+".";
		}
		this.tableName += tblName.toUpperCase();
	}
	//注解Insert的属性
	private InsertAttribute insertAttribute;
	//注解Query的属性
	private QueryAttribute queryAttribute;
	//注解Update的属性
	private UpdateAttribute updateAttribute;

	private boolean isTree;

	private String parentFieldName;

	private String leftFieldName;

	private String rightFieldName;
	
	public String getTableName() {
		return tableName;
	}
	public Set<String> getGeneratedKeyColumns() {
		return generatedKeyColumns;
	}
	public void setGeneratedKeyColumns(List<String> generatedKeyColumns) {
		if(null == generatedKeyColumns || generatedKeyColumns.size()==0){
			return;
		}
		this.generatedKeyColumns = New.set(generatedKeyColumns.size());
		for(String s : generatedKeyColumns){
			this.generatedKeyColumns.add(s.toUpperCase());
		}
	}
	public String getPkColumns() {
		return pkColumns;
	}
	/**
	 * 设置主键，若主键为复合键，则用逗号分隔
	 * @param pkColumns
	 */
	public void setPkColumns(String pkColumns) {
		this.pkColumns = pkColumns;
	}
	public List<String> getUkColumns() {
		return ukColumns;
	}
	/**
	 * 设置表的唯一约束键，若唯一约束键为复合键，则用逗号隔开。
	 * @param ukColumns
	 */
	public void setUkColumns(List<String> ukColumns) {
		this.ukColumns = ukColumns;
	}
	public Map<String, Column> getColumns() {
		return columns;
	}
	public void setColumns(Map<String, Column> columns) {
		this.columns = columns;
	}
    public Column getColumn(String key){
        return columns.get(key);
    }
	public InsertAttribute getInsertAttribute() {
		return insertAttribute;
	}
	public void setInsertAttribute(InsertAttribute insertAttribute) {
		this.insertAttribute = insertAttribute;
	}
	private Map<String, String> entityAttributeColumnMap;
	/**
	 * 获取实体属性与数据库字段的映射关系
	 */
	public Map<String, String> getEntityAttributeColumnMap() {
		return entityAttributeColumnMap;
	}
	public void setEntityAttributeColumnMap(Map<String, String> entityKeyMap) {
		this.entityAttributeColumnMap = entityKeyMap;
	}
	public QueryAttribute getQueryAttribute() {
		return queryAttribute;
	}
	public void setQueryAttribute(QueryAttribute queryAttribute) {
		this.queryAttribute = queryAttribute;
	}
	/**
	 * 注解Update的属性
	 * @return
	 */
	public UpdateAttribute getUpdateAttribute() {
		return updateAttribute;
	}
	/**
	 * 注解Update的属性
	 * @param updateAttribute
	 */
	public void setUpdateAttribute(UpdateAttribute updateAttribute) {
		this.updateAttribute = updateAttribute;
	}

	public boolean isTree() {
		return isTree;
	}
	public void setTree(boolean isTree) {
		this.isTree = isTree;
	}

	public String getParentFieldName() {
		return parentFieldName;
	}
	public void setParentFieldName(String parentFieldName) {
		this.parentFieldName = parentFieldName;
	}

	public String getLeftFieldName() {
		return leftFieldName;
	}
	public void setLeftFieldName(String leftFieldName) {
		this.leftFieldName = leftFieldName;
	}

	public String getRightFieldName() {
		return rightFieldName;
	}
	public void setRightFieldName(String rightFieldName) {
		this.rightFieldName = rightFieldName;
	}
}
